# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Contributor: Konstantin Gizdov <arch at kge dot pw>

pkgname=tensorboard
pkgver=2.15.0
pkgrel=1
pkgdesc="TensorFlow's Visualization Toolkit"
url='https://github.com/tensorflow/tensorboard'
arch=('loong64' 'x86_64')
license=('APACHE')
depends=('python' 'python-werkzeug' 'python-numpy' 'python-bleach' 'python-wheel' 'python-markdown'
         'python-html5lib' 'python-protobuf' 'python-grpcio' 'python-google-auth-oauthlib'
         'python-tensorboard_plugin_wit' 'absl-py')
makedepends=('bazel' 'python-setuptools' 'tree' 'rust' 'python-build' 'python-installer' 'python-wheel' 'git')
optdepends=('python-tensorflow: advanced features for TensorBoard')
source=(
  "${pkgname}-${pkgver}.tar.gz::https://github.com/tensorflow/tensorboard/archive/${pkgver}.tar.gz"
  https://github.com/bazelbuild/bazel/releases/download/5.4.0/bazel_nojdk-5.4.0-linux-x86_64
)
options=('!lto')
sha512sums=('9b6927e23f9ab6af04c8872e3b98472b7af47e447ee931e3c0d8c371ca89f5e418342197497da71db10c926cae790c8f96fd10465d6a2b255658e4a4deed1581'
            'e2adb747cd1fe3c90686831703618af3f8bc8197a96d9e1e90e66db38dbc4e7a94d88dac755b25e288002983a87fcffbfb0d7c2e356d979d4635301c3daf9281')

prepare() {
  # Since tensorboard is currently imcompatible with Bazel 6, we're going to use
  # a local Bazel 5 to fix that. Stupid problems call for stupid solutions.
  install -Dm755 "${srcdir}"/bazel_nojdk-5.4.0-linux-x86_64 bazel/bazel
  export PATH="${srcdir}/bazel:$PATH"
  bazel --version

  cd "$pkgname-$pkgver"

  # Remove fixed version requirements as we usually ship more up-to-date
  # versions than are specified and it tends to work fine anyhow.
  sed -i "s/[<>=].*//" tensorboard/pip_package/requirements.txt
}

build() {
  cd "$pkgname-$pkgver"

  PYTHONWARNINGS=ignore bazel build //tensorboard
  bazel build //tensorboard/pip_package:build_pip_package

  cd tensorboard/data/server
  cargo build --release
  python pip_package/build.py --server-binary target/release/rustboard --out-dir /tmp
  cd "$srcdir/$pkgname-$pkgver"

  mkdir pypackage
  cp -R bazel-bin/tensorboard/pip_package/build_pip_package.runfiles/org_tensorflow_tensorboard/external pypackage
  cp -R bazel-bin/tensorboard/pip_package/build_pip_package.runfiles/org_tensorflow_tensorboard/tensorboard pypackage
  cp tensorboard/pip_package/{README.rst,MANIFEST.in,setup.py,requirements.txt} pypackage
  cp README.md pypackage
  cd pypackage
  python -m build --wheel --no-isolation
}

package() {
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd "$pkgname-$pkgver"

  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"

  python -m installer --destdir="$pkgdir" /tmp/*.whl

  cd pypackage
  python -m installer --destdir="$pkgdir" dist/*.whl

  rm -r "$pkgdir/$site_packages/external/"
}

# vim:set ts=2 sw=2 et:
